package com.ptu.spzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ptu.spzx.common.exception.ptuException;
import com.ptu.spzx.domain.Delete.DeleteIds;
import com.ptu.spzx.domain.Knowledge.KnowledgePoint;
import com.ptu.spzx.domain.Question.po.QuestionKnowledge;
import com.ptu.spzx.mapper.KnowledgePointMapper;
import com.ptu.spzx.mapper.QuestionKnowledgeMapper;
import com.ptu.spzx.mapper.QuestionMapper;
import com.ptu.spzx.model.vo.common.ResultCodeEnum;
import com.ptu.spzx.service.IKnowledgePointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YT
 * @since 2024-01-28
 */
@Service
public class KnowledgePointServiceImpl extends ServiceImpl<KnowledgePointMapper, KnowledgePoint> implements IKnowledgePointService {

    @Autowired
    private KnowledgePointMapper knowledgePointMapper;
    @Autowired
    private QuestionKnowledgeMapper questionKnowledgeMapper;
    @Autowired
    private QuestionMapper questionMapper;
    /**
     * 新增知识点
     * @param knowledgePoint
     */
    @Override
    public void addKnowledgePoint(KnowledgePoint knowledgePoint) {
        if(knowledgePoint==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        knowledgePointMapper.insert(knowledgePoint);
    }

    /**
     *
     * @param knowledgePoint
     */
    @Override
    public void updateKnowledgePoint(KnowledgePoint knowledgePoint) {
        if(knowledgePoint.getKonwledgeId()==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        knowledgePointMapper.updateById(knowledgePoint);
    }


    /**
     * 批量删除知识点
     * @param
     */
    @Override
    public void deleteKnowledgePointByIds(DeleteIds deleteIds) {
        List<Long> ids = deleteIds.getIds();
        if(ids==null||ids.size()==0){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        knowledgePointMapper.deleteBatchIds(ids);
        List<Long> questionIds=new ArrayList<>();
        for(Long id:ids){
            List<Long> questionId = questionKnowledgeMapper.selectByKnowledge(id);
            questionIds.addAll(questionId);
        }
        if(questionIds!=null){
            LambdaQueryWrapper<QuestionKnowledge> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(QuestionKnowledge::getQuestionId,questionIds);
            questionKnowledgeMapper.delete(lambdaQueryWrapper);
            questionMapper.deleteBatchIds(questionIds);
        }
    }



    /**
     * 查询单条知识点
     * @param id
     * @return
     */
    @Override
    public KnowledgePoint QueryKnowledgePoint(Long id) {
        if(id==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        KnowledgePoint knowledgePoint = knowledgePointMapper.selectById(id);
        return knowledgePoint;
    }

    /**
     *  根据课程批量查询知识点
     * @param belongCourseId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<KnowledgePoint> QueryKnowledgePointByBelongCourseIds(Long belongCourseId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<KnowledgePoint> knowledgePointLambdaQueryWrapper=new LambdaQueryWrapper<>();
        knowledgePointLambdaQueryWrapper.eq(KnowledgePoint::getBelongCourseId,belongCourseId);
        PageHelper.startPage(pageNum,pageSize);
        List<KnowledgePoint> knowledgePoints = knowledgePointMapper.selectList(knowledgePointLambdaQueryWrapper);
        PageInfo<KnowledgePoint> pageInfo=new PageInfo<>(knowledgePoints);
        return pageInfo;
    }

    @Override
    public PageInfo<KnowledgePoint> orderByKnowledgeUsed(Integer pageSize, Integer pageNumber) {
        LambdaQueryWrapper<KnowledgePoint> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(KnowledgePoint::getKonwledgeUsed);
        PageHelper.startPage(pageNumber,pageSize);
        List<KnowledgePoint> knowledgePoints = knowledgePointMapper.selectList(lambdaQueryWrapper);
        PageInfo<KnowledgePoint> pageInfo=new PageInfo<>(knowledgePoints);
        return pageInfo;
    }
}
